package aiku.numericalcontroloa.Service.DepartmentManagement.Impl;

import aiku.numericalcontroloa.Base.Utils.HttpStatus;
import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Mapper.DepartmentManagement.DepartmentMapper;
import aiku.numericalcontroloa.Mapper.DepartmentManagement.EmployeeDepartmentMapper;
import aiku.numericalcontroloa.Model.Dto.DepartmentManagement.EmployeeDepartmentDto;
import aiku.numericalcontroloa.Model.Dto.PageSelect;
import aiku.numericalcontroloa.Model.Entity.DepartmentManagement.Department;
import aiku.numericalcontroloa.Model.Entity.DepartmentManagement.EmployeeDepartment;
import aiku.numericalcontroloa.Model.Entity.DepartmentManagement.EmployeeDepartmentWithUser;
import aiku.numericalcontroloa.Model.Vo.DepartmentManagement.EmployeeDepartmentVo;
import aiku.numericalcontroloa.Service.DepartmentManagement.EmployeeDepartmentService;
import aiku.numericalcontroloa.project.system.mapper.SysRoleMapper;
import aiku.numericalcontroloa.project.system.pojo.entity.SysRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author 黄广庭
 * @Date 2024/2/22 13:41
 */
@Service
public class EmployeeDepartmentServiceImpl implements EmployeeDepartmentService {
    private final EmployeeDepartmentMapper employeeDepartmentMapper;
    private final SysRoleMapper sysRoleMapper;
    private final DepartmentMapper departmentMapper;

    public EmployeeDepartmentServiceImpl(EmployeeDepartmentMapper employeeDepartmentMapper, SysRoleMapper sysRoleMapper, DepartmentMapper departmentMapper) {
        this.employeeDepartmentMapper = employeeDepartmentMapper;
        this.sysRoleMapper = sysRoleMapper;
        this.departmentMapper = departmentMapper;
    }


    @Override
    public Result addEmployeeDepartmentList(EmployeeDepartmentDto employeeDepartmentDto) {
        if (employeeDepartmentDto == null){
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        }
        if (employeeDepartmentDto.getIdList() == null || employeeDepartmentDto.getIdList().isEmpty()){
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        }
        List<EmployeeDepartment> employeeDepartmentList = new ArrayList<>();
        for (String id : employeeDepartmentDto.getIdList()){
            EmployeeDepartment employeeDepartment = new EmployeeDepartment();
            employeeDepartment.setId(UUID.randomUUID().toString());
            employeeDepartment.setEmployeeId(id);
            employeeDepartment.setDepartmentId(employeeDepartmentDto.getDepartmentId());
            employeeDepartmentList.add(employeeDepartment);
        }
        employeeDepartmentMapper.insertBatch(employeeDepartmentList);
        return Result.success("添加成功");
    }

    @Override
    public Result getEmployeeDepartmentList(EmployeeDepartmentDto employeeDepartmentDto) {
        if (employeeDepartmentDto == null){
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        }
        PageSelect pageSelect = new PageSelect();
        if (employeeDepartmentDto.getCurrentPage() != null && employeeDepartmentDto.getPageSize() != null){
            pageSelect.setCurrentPage(employeeDepartmentDto.getCurrentPage());
            pageSelect.setPageSize(employeeDepartmentDto.getPageSize());
            pageSelect.setOffset((employeeDepartmentDto.getCurrentPage()*employeeDepartmentDto.getPageSize())-employeeDepartmentDto.getPageSize());
        }
        // 1、查询该部门的所有下级部门，将id放进数组中
        List<String> departmentIdList = new ArrayList<>();
        departmentIdList.add(employeeDepartmentDto.getDepartmentId());
        getDepartmentIdList(departmentIdList,departmentIdList);
        // 去重
        Set<String> departmentIds = new HashSet<>(departmentIdList);
        System.out.println("部门id==>"+departmentIds);
        
        // 2、根据部门id查询所有员工
        QueryWrapper<EmployeeDepartmentWithUser> employeeDepartmentLQW = new QueryWrapper<>();
        // 根据工号或姓名查询
        if (employeeDepartmentDto.getNickName() != null && !employeeDepartmentDto.getNickName().isEmpty()){
            employeeDepartmentLQW.like("u.nick_name",employeeDepartmentDto.getNickName())
                    .in("department_id",departmentIds)
                    .or()
                    .like("u.empno",employeeDepartmentDto.getNickName())
                    .in("department_id",departmentIds);
        }else {
            employeeDepartmentLQW.lambda().in(EmployeeDepartment::getDepartmentId,departmentIds);
        }
        List<EmployeeDepartmentWithUser> employeeDepartmentList = employeeDepartmentMapper.selectListWithUser(pageSelect, employeeDepartmentLQW.lambda());
        Integer total = employeeDepartmentMapper.selectListWithUserTotalAll(employeeDepartmentLQW.lambda());
        List<EmployeeDepartmentVo> employeeDepartmentVoList = new ArrayList<>();
        if (employeeDepartmentList.size() > 0){
            for (EmployeeDepartmentWithUser item : employeeDepartmentList){
                EmployeeDepartmentVo employeeDepartmentVo = new EmployeeDepartmentVo();
                BeanUtils.copyProperties(item,employeeDepartmentVo);
                SysRole sysRole = sysRoleMapper.selectById(item.getRoleId());
                employeeDepartmentVo.setRoleName(sysRole.getNameZh());
                employeeDepartmentVoList.add(employeeDepartmentVo);
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("employeeDepartmentVoList",employeeDepartmentVoList);
        return Result.success("查询成功",map);
    }

    @Override
    public Result deleteEmployeeDepartmentList(EmployeeDepartmentDto employeeDepartmentDto) {
        if (employeeDepartmentDto == null){
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        }
        if (employeeDepartmentDto.getIdList() == null || employeeDepartmentDto.getIdList().isEmpty()){
            return new Result(HttpStatus.BAD_REQUEST,"缺少参数");
        }
        employeeDepartmentMapper.deleteBatchIds(employeeDepartmentDto.getIdList());
        return Result.success("删除成功");
    }

    public List<String> getDepartmentIdList(List<String> departmentIdList,List<String> idList){
        LambdaQueryWrapper<Department> departmentLQW = new LambdaQueryWrapper<>();
        departmentLQW.in(Department::getParentDepartmentId,idList);
        List<Department> departmentList = departmentMapper.selectList(departmentLQW);
        if (departmentList.size() > 0){
            List<String> childrenIdList = departmentList.stream().map(Department::getId).collect(Collectors.toList());
            departmentIdList.addAll(childrenIdList);
            departmentIdList.addAll(getDepartmentIdList(departmentIdList,childrenIdList));
        }
        return departmentIdList;
    }
}
